# coding:utf-8

from flask import request
from flask_restful import Api, Resource

from common.im_util import im_client, CS_ACCID2
from app.op.models import User
from .views import Response
from .codes import SUCCESS, PARAMS_ILLEGAL
from .im_schemas import im_welcome_schema

api = Api()


@api.resource('/Im/csadSendWelcomeIm')
class CSIMWelcomeResource(Resource):
    """
    created by chenhj at 2017/8/22

    """
    def post(self):
        data, errors = im_welcome_schema.load(request.form)
        if errors:
            return Response(PARAMS_ILLEGAL.new_format_msg(list(errors.values())[0][0]))

        user = User.get_by_id(data['uid'])
        # FIXME by chenhj 这里的客服id硬编码为4
        im_client.single_send_text_msg(CS_ACCID2, user.id, data['msg'])
        return Response(SUCCESS)
